home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap04 / FollowingAgent.java < prev    next >
Text File  |  1996-09-19  |  2KB  |  52 lines

  1. //
  2. // an agent is always following you.
  3. //
  4.  
  5. import vrml.*;
  6. import vrml.node.*;
  7. import vrml.field.*;
  8.  
  9. public class FollowingAgent extends Script{
  10.     SFVec3f setAgentPosition;
  11.     float yourPosition[] = new float[3];
  12.     float agentPosition[] = new float[3];
  13.     
  14.     // the agent's position when you are at (0, 0, 0).
  15.     final float agentDefaultPositionX = 2.0f;
  16.     final float agentDefaultPositionY = -1.4f;
  17.     final float agentDefaultPositionZ = -6.0f;
  18.     
  19.     public void initialize(){
  20.         // get the reference of the event out 'setAgentPosition'.
  21.         setAgentPosition = (SFVec3f)getEventOut("setAgentPosition");
  22.     }
  23.     
  24.     public void processEvent(Event e){
  25.         if(e.getName().equals("currentPosition") == true){
  26.  
  27.             // record your current position.
  28.             ((ConstSFVec3f)e.getValue()).getValue(yourPosition);
  29.  
  30.             // move the agent.
  31.             moveAgent();
  32.         }
  33.     }
  34.     
  35.     // move the agent just around you.
  36.     void moveAgent(){
  37.         // translate the agent according to your current position.
  38.         agentPosition[0] = yourPosition[0] + agentDefaultPositionX;
  39.         agentPosition[1] = yourPosition[1]+ agentDefaultPositionY;
  40.         agentPosition[2] = yourPosition[2]+ agentDefaultPositionZ;
  41.         
  42.         // debug
  43.         System.out.println("moveAgent : your current position is (" +
  44.                            yourPosition[0] + ", " +
  45.                            yourPosition[1] + ", " +
  46.                            yourPosition[2] + ")");
  47.  
  48.         // move the agent to the new position.
  49.         setAgentPosition.setValue(agentPosition);
  50.     }
  51. }
  52.